<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>代码生成结果</title>
    <link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
    <link th:href="@{/css/bootstrap-icons.css}" rel="stylesheet">
    <link th:href="@{/css/prism-tomorrow.min.css}" rel="stylesheet">
    <style>
        :root {
            --bs-primary: #3f51b5;
            --bs-primary-rgb: 63, 81, 181;
            --bs-success: #4caf50;
            --bs-success-rgb: 76, 175, 80;
            --bs-secondary: #6c757d;
        }
        body {
            background-color: #f8f9fa;
        }
        .result-container {
            max-width: 1300px;
            margin: 0 auto;
            padding: 30px 20px;
        }
        .code-container {
            border-radius: 8px;
            margin-bottom: 20px;
            overflow: hidden;
            box-shadow: 0 2px 8px rgba(0,0,0,0.08);
        }
        pre {
            margin: 0;
            padding: 15px;
            font-size: 14px;
            white-space: pre-wrap;
            max-height: 400px;
            overflow-y: auto;
        }
        .file-header {
            background-color: #2d2d2d;
            color: #e0e0e0;
            padding: 10px 15px;
            font-weight: 500;
            font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .file-header .btn {
            font-size: 0.8rem;
            padding: 0.2rem 0.5rem;
        }
        .nav-tabs {
            border-bottom: none;
        }
        .nav-tabs .nav-link {
            border-radius: 8px 8px 0 0;
            margin-right: 4px;
            font-weight: 500;
            padding: 10px 20px;
        }
        .nav-tabs .nav-link.active {
            background-color: white;
            border-bottom-color: white;
            color: var(--bs-primary);
        }
        .tab-content {
            border-radius: 0 8px 8px 8px;
            padding: 25px;
            background-color: #fff;
            box-shadow: 0 4px 12px rgba(0,0,0,.05);
        }
        .tree-view {
            font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
            white-space: pre;
            line-height: 1.5;
            margin-left: 20px;
            font-size: 0.9rem;
        }
        .table-metadata {
            background-color: #fff;
            border-radius: 12px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 4px 12px rgba(0,0,0,.05);
            border-left: 4px solid var(--bs-primary);
        }
        .table-badge {
            font-size: 0.8rem;
            margin-right: 6px;
            padding: 5px 10px;
            font-weight: normal;
        }
        .table-details {
            margin-top: 15px;
        }
        .action-row {
            background-color: white;
            border-radius: 12px;
            padding: 15px 20px;
            box-shadow: 0 4px 12px rgba(0,0,0,.05);
        }
        .btn-primary {
            background-color: var(--bs-primary);
            border-color: var(--bs-primary);
        }
        .btn-success {
            background-color: var(--bs-success);
            border-color: var(--bs-success);
        }
        .icon-circle {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            width: 28px;
            height: 28px;
            border-radius: 50%;
            background-color: rgba(var(--bs-primary-rgb), 0.1);
            color: var(--bs-primary);
            margin-right: 10px;
        }
        .success-icon {
            background-color: rgba(var(--bs-success-rgb), 0.1);
            color: var(--bs-success);
            font-size: 1.2rem;
        }
        .filter-bar {
            background-color: white;
            border-radius: 8px;
            padding: 10px 15px;
            margin-bottom: 15px;
            display: flex;
            align-items: center;
            box-shadow: 0 2px 5px rgba(0,0,0,.05);
        }
        .filter-input {
            border: none;
            outline: none;
            background: transparent;
            width: 100%;
            padding: 5px;
        }
        .copy-btn:hover {
            background-color: rgba(255,255,255,0.1);
        }
        .file-count {
            font-size: 0.9rem;
            color: #6c757d;
            margin-top: 5px;
        }
        .framework-badge {
            font-size: 0.9rem;
            padding: 6px 12px;
            margin-left: 10px;
        }
    </style>
</head>
<body>
    <div class="result-container">
        <div class="mb-4 d-flex justify-content-between align-items-center">
            <div>
                <h1 class="display-6 fw-bold mb-1">
                    代码生成结果
                    <span th:if="${frameworkType == 'SPRING_BOOT'}" class="badge bg-success fw-normal framework-badge">
                        <i class="bi bi-box me-1"></i> Spring Boot 3.3.0
                    </span>
                    <span th:if="${frameworkType == 'COLA'}" class="badge bg-info fw-normal framework-badge">
                        <i class="bi bi-box me-1"></i> COLA 架构
                    </span>
                </h1>
                <p class="text-secondary mb-0">生成时间: <span th:text="${#dates.format(#dates.createNow(), 'yyyy-MM-dd HH:mm:ss')}"></span></p>
            </div>
            <a href="/cola-generator" class="btn btn-outline-primary">
                <i class="bi bi-arrow-left me-1"></i> 返回生成器
            </a>
        </div>
        
        <div class="alert alert-success d-flex align-items-center mb-4" role="alert">
            <div class="icon-circle success-icon me-3">
                <i class="bi bi-check-lg"></i>
            </div>
            <div>
                <h4 class="alert-heading mb-1">生成成功！</h4>
                <p class="mb-0">已成功生成 <strong th:text="${result.tableMetaDataList.size()}"></strong> 个表的代码，共 <strong th:text="${result.generatedFiles.size()}"></strong> 个文件。</p>
            </div>
        </div>
        
        <!-- 表信息摘要 -->
        <h5 class="mb-3">
            <i class="bi bi-table me-2"></i> 已处理的表 (<span th:text="${result.tableMetaDataList.size()}"></span>)
        </h5>
        <div class="row g-3 mb-4">
            <div th:each="table : ${result.tableMetaDataList}" class="col-md-6">
                <div class="table-metadata">
                    <div class="d-flex justify-content-between align-items-center">
                        <h5 class="mb-0">
                            <i class="bi bi-database me-2"></i>
                            <span th:text="${table.tableName}"></span>
                        </h5>
                        <span class="badge bg-primary rounded-pill" th:text="${table.fields.size()} + ' 字段'"></span>
                    </div>
                    <div class="mt-2 d-flex flex-wrap">
                        <span th:if="${table.hasDateFields}" class="badge bg-info text-dark table-badge">
                            <i class="bi bi-calendar me-1"></i> 日期字段
                        </span>
                        <span th:if="${table.hasDecimalFields}" class="badge bg-info text-dark table-badge">
                            <i class="bi bi-calculator me-1"></i> 小数字段
                        </span>
                        <span th:if="${table.hasStatusField}" class="badge bg-info text-dark table-badge">
                            <i class="bi bi-toggle-on me-1"></i> 状态字段
                        </span>
                    </div>
                    <div class="table-details">
                        <div class="mb-2">
                            <span class="text-secondary">表注释：</span> 
                            <span th:text="${table.tableComment}?: '无'"></span>
                        </div>
                        <div>
                            <span class="text-secondary">领域对象名：</span> 
                            <span class="fw-medium" th:text="${table.domainObjectName}"></span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <ul class="nav nav-tabs mb-0" id="resultTab" role="tablist">
            <li class="nav-item" role="presentation">
                <button class="nav-link active d-flex align-items-center" id="files-tab" data-bs-toggle="tab" data-bs-target="#files" type="button" role="tab" aria-controls="files" aria-selected="true">
                    <i class="bi bi-code-square me-2"></i> 生成的文件
                </button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link d-flex align-items-center" id="structure-tab" data-bs-toggle="tab" data-bs-target="#structure" type="button" role="tab" aria-controls="structure" aria-selected="false">
                    <i class="bi bi-diagram-3 me-2"></i> 项目结构
                </button>
            </li>
        </ul>
        
        <div class="tab-content" id="resultTabContent">
            <!-- 生成的文件 -->
            <div class="tab-pane fade show active" id="files" role="tabpanel" aria-labelledby="files-tab">
                <div class="filter-bar">
                    <i class="bi bi-search me-2"></i>
                    <input type="text" id="fileFilter" class="filter-input" placeholder="搜索文件名...">
                </div>
                
                <div th:if="${result.generatedFiles.isEmpty()}">
                    <div class="alert alert-info" role="alert">
                        <i class="bi bi-info-circle me-2"></i> 没有生成任何文件。
                    </div>
                </div>
                <div th:unless="${result.generatedFiles.isEmpty()}" id="filesList">
                    <div th:each="file : ${result.generatedFiles}" class="code-container file-item">
                        <div class="file-header">
                            <span th:text="${file.key}"></span>
                            <button class="btn btn-sm btn-outline-light copy-btn" 
                                    onclick="copyCode(this)" 
                                    th:data-code="${file.value}">
                                <i class="bi bi-clipboard"></i> 复制
                            </button>
                        </div>
                        <pre><code class="language-java" th:text="${file.value}"></code></pre>
                    </div>
                    <p class="file-count">显示 <span id="visibleFilesCount" th:text="${result.generatedFiles.size()}"></span> 个文件（共 <span th:text="${result.generatedFiles.size()}"></span> 个）</p>
                </div>
            </div>
            
            <!-- 项目结构 -->
            <div class="tab-pane fade" id="structure" role="tabpanel" aria-labelledby="structure-tab">
                <div class="bg-light p-3 rounded mb-4">
                    <div class="tree-view">
                        <div th:each="line : ${result.projectStructure}" th:text="${line}"></div>
                    </div>
                </div>
                
                <div class="card border-0 shadow-sm">
                    <div class="card-header bg-white">
                        <h5 class="mb-0">
                            <i class="bi bi-lightbulb me-2"></i> 如何使用生成的代码
                        </h5>
                    </div>
                    <div class="card-body">
                        <ol class="mb-0">
                            <li class="mb-2">创建标准的COLA项目结构（如果尚未创建）</li>
                            <li class="mb-2">将生成的代码按照以上结构复制到对应的模块和包中</li>
                            <li class="mb-2">添加必要的依赖，包括COLA框架、Spring Boot、MyBatis等</li>
                            <li>调整代码以适应您的具体业务需求</li>
                        </ol>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="mt-4 action-row">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <h5 class="mb-0">需要下载生成的代码吗？</h5>
                    <p class="mb-0 text-secondary">所有文件将打包为ZIP格式下载</p>
                </div>
                <button id="downloadBtn" class="btn btn-success px-4">
                    <i class="bi bi-download me-2"></i> 下载代码包
                </button>
            </div>
        </div>
    </div>
    
    <script th:src="@{/js/bootstrap.bundle.min.js}"></script>
    <script th:src="@{/js/prism.min.js}"></script>
    <script th:src="@{/js/prism-java.min.js}"></script>
    <script th:src="@{/js/jszip.min.js}"></script>
    <script th:src="@{/js/FileSaver.min.js}"></script>
    
    <script th:inline="javascript">
        // 获取生成的文件数据
        const generatedFiles = /*[[${result.generatedFiles}]]*/ {};
        
        // 下载所有文件
        document.getElementById('downloadBtn').addEventListener('click', function() {
            const zip = new JSZip();
            
            // 为每个文件创建适当的文件夹结构
            for (const [filePath, content] of Object.entries(generatedFiles)) {
                const cleanPath = filePath.replace(/^\/\/\s*/, '').trim();
                zip.file(cleanPath, content);
            }
            
            // 显示下载进度
            this.innerHTML = '<i class="bi bi-hourglass-split me-2"></i> 准备下载...';
            this.disabled = true;
            
            // 生成并下载ZIP文件
            zip.generateAsync({ type: 'blob' }).then(function(content) {
                saveAs(content, 'ColaCode.zip');
                
                // 恢复按钮状态
                document.getElementById('downloadBtn').innerHTML = '<i class="bi bi-download me-2"></i> 下载代码包';
                document.getElementById('downloadBtn').disabled = false;
            });
        });
        
        // 复制代码
        function copyCode(button) {
            const code = button.getAttribute('data-code');
            navigator.clipboard.writeText(code).then(function() {
                // 更改按钮显示为已复制
                const originalHtml = button.innerHTML;
                button.innerHTML = '<i class="bi bi-check"></i> 已复制';
                
                // 2秒后恢复原样
                setTimeout(() => {
                    button.innerHTML = originalHtml;
                }, 2000);
            });
        }
        
        // 文件过滤器
        document.getElementById('fileFilter').addEventListener('input', function() {
            const filterText = this.value.toLowerCase();
            const fileItems = document.getElementsByClassName('file-item');
            let visibleCount = 0;
            
            Array.from(fileItems).forEach(item => {
                const fileName = item.querySelector('.file-header span').textContent.toLowerCase();
                if (fileName.includes(filterText)) {
                    item.style.display = 'block';
                    visibleCount++;
                } else {
                    item.style.display = 'none';
                }
            });
            
            // 更新可见文件计数
            document.getElementById('visibleFilesCount').textContent = visibleCount;
        });
        
        // 语法高亮
        document.addEventListener('DOMContentLoaded', function() {
            Prism.highlightAll();
        });
    </script>
</body>
</html> 